#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<errno.h>
#include<signal.h>
#include<sys/time.h>
#include<fcntl.h>
#include<unistd.h> 
#include<pthread.h>

pthread_mutex_t mutex;


void* myfunc(void*){
   while(1){
        sleep(rand() % 3);
        pthread_mutex_lock(&mutex);
        printf("hellow\n");
        sleep(rand() % 3);
        printf("world\n");
        pthread_mutex_unlock(&mutex);
        sleep(rand() % 3);
   } 
}
int main(int argc, char* args[])
{
    int ret;
    pthread_t tid;
    ret = pthread_mutex_init(&mutex, NULL);
    if(ret){
        fprintf(stderr, "pthread_mutex_init error", strerror(ret));
        exit(1);
    }

    ret = pthread_create(&tid, NULL, myfunc, NULL);
    if(ret){
        fprintf(stderr, "pthread_create error", strerror(ret));
        exit(1);
    }

    while(1){
        sleep(rand() % 3);
        pthread_mutex_lock(&mutex);
        printf("HELLOW\n");
        sleep(rand() % 3);
        printf("WORLD\n");
        pthread_mutex_unlock(&mutex);
        sleep(rand() % 3);
    }
    
    ret = pthread_join(tid, NULL);
    if(ret){
        fprintf(stderr, "pthread_join error", strerror(ret));
        exit(1);
    }

    pthread_mutex_destroy(&mutex);

    return 0;
}